package com.kunkun;

import javax.swing.*;

public class Image {
    private static final String imagePath = "stone_maze/src/image/";
    private MainFrame mainFrame;
    private int[][] imageArray;
    private int[][] winArray=new int[][] {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };
    //定义有参构造器
    public Image(MainFrame mainFrame,int[][] imageArray) {
        this.mainFrame = mainFrame;
        this.imageArray = imageArray;
        initImage();
    }
    private void initImage(){
        //先清空窗口内放入所有图层
        mainFrame.getContentPane().removeAll();
        //判断是否胜利
        if(isWin()){
            JLabel label = new JLabel(new ImageIcon(imagePath + "win.png"));
            label.setBounds(124,230,266,88);
            mainFrame.add(label);
            mainFrame.repaint();
        }
        //展示一个行列矩阵的图片色块依次填满窗口(4*4)
        for (int i = 0; i < imageArray.length; i++) {
            for (int j = 0; j < imageArray[i].length; j++) {
                //创建一个图片对象
                JLabel label = new JLabel();
                //设置图片路径
                label.setIcon(new ImageIcon(imagePath + imageArray[i][j] + ".png"));
                //设置图片位置
                label.setBounds(20+j * 100, 60+i * 100, 100, 100);
                //将图片添加到窗口中
                mainFrame.add(label);
            }
        }
        //设置窗口的背景图片
        JLabel background = new JLabel(new ImageIcon(imagePath + "background.png"));
        background.setBounds(0,0,450,484);
        mainFrame.add(background);
        //刷新图层
        mainFrame.repaint();
    }

    private boolean isWin() {
        for (int i = 0; i < imageArray.length; i++) {
            for (int j = 0; j < imageArray[i].length; j++) {
                if(imageArray[i][j] != winArray[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
}
